package com.app.service.student.impl;

import com.app.mapper.Information.GradeMapper;
import com.app.mapper.student.management.ManagementMapper;
import com.app.pojo.Grade;
import com.app.pojo.Student;
import com.app.service.student.ManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@Transactional
public class ManagementServiceImpl implements ManagementService {

    @Autowired
    ManagementMapper managementMapper;

    @Autowired
    GradeMapper gradeMapper;

    @Override
    public Student getStudentById(Integer id) {
        return managementMapper.getStudentById(id);
    }

    @Override
    public String updatePassword(Integer id, String oldPassword ,String newPassword) {
        Student student = managementMapper.getStudentById(id);
        if(!student.getPassword().equals(oldPassword)) {
            return "旧密码不正确";
        }
        Integer rows = managementMapper.updatePassword(id,newPassword);
        if(rows!=1){
            return "修改密码未知异常";
        }
        return "密码修改成功,请重新登录";
    }

    @Override
    public String updateInfo(Integer id, String name, Integer sex, String mobilePhone) {
        if(id==null || name==null || name=="" ||
                sex==null || mobilePhone==null || mobilePhone==""){
            return "用值为空";
        }
        Integer rows =managementMapper.updateInfo(id,name,sex,mobilePhone);
        if(rows!=1){
            return "修改时未知异常";
        }
        return "修改成功";
    }

    @Override
    public List<Student> getStudents(String teacherId,String name, Integer sex, String major, String year) {
        Student student = new Student();
        student.setName(name);
        student.setSex(sex);
        student.setMajor(major);
        student.setYear(year);
        student.setTeacherId(teacherId);
        return managementMapper.getStudents(student);
    }

    @Override
    public Grade getGrade(String studentId){
        return managementMapper.getGrade(studentId);
    }

    @Override
    public Integer updateStudent(Student student){
        return  managementMapper.updateStudent(student);
    }
    @Override
    public Integer addStudent(Student student){
        gradeMapper.addGradeToStudent(student.getUserId(),student.getName());
        return  managementMapper.addStudent(student);
    }

    @Override
    public Integer delStudent(Integer id) {
        return managementMapper.delStudent(id);
    }
}
